package cn.hanyx.coder.design.pattern.structural.proxy.demo02;

import java.lang.reflect.Proxy;

/**
 * MyDynamicProxy
 *
 * @author hanyx
 * @date 2020/09/02
 */
public class MyDynamicProxy {

    public static void main(String[] args) {
        Hello hello = new HelloImpl();
        MyInvocationHandler handler = new MyInvocationHandler(hello);
        // 构造代码实例
        Hello proxyHello = (Hello) handler.newProxyInstance(HelloImpl.class.getClassLoader(), HelloImpl.class.getInterfaces());
        // 调用代理方法
        proxyHello.sayHello();

        Hello hello02 = new HelloImpl02();
        handler = new MyInvocationHandler(hello02);
        proxyHello = (Hello) handler.newProxyInstance(HelloImpl02.class.getClassLoader(), HelloImpl02.class.getInterfaces());
        proxyHello.sayHello();

        Hello02 hello03 = new HelloImpl03();
        handler = new MyInvocationHandler(hello03);
        Hello02 proxyHello02 = (Hello02) handler.newProxyInstance(HelloImpl03.class.getClassLoader(), HelloImpl03.class.getInterfaces());
        proxyHello02.sayHello();

    }
}
